package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.SystemColor;
import java.text.NumberFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.ImageIcon;

import controller.KhachHangController;
import controller.UserController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class KhachHang extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					KhachHang frame = new KhachHang("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public KhachHang( final String user) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 688, 469);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(new LineBorder(new Color(184,
				207, 229)), "", TitledBorder.LEFT, TitledBorder.ABOVE_TOP,
				null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new TitledBorder(new LineBorder(
				new Color(184, 207, 229)), "", TitledBorder.LEFT,
				TitledBorder.TOP, null, null),
				"Qu\u1EA3n l\u00FD kh\u00E1ch h\u00E0ng", TitledBorder.LEFT,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		panel.setBounds(10, 11, 652, 89);
		contentPane.add(panel);
		panel.setLayout(null);

		JButton btnThm = new JButton("Thêm");
		btnThm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
					ThemKhachHang a = new ThemKhachHang();
					a.setVisible(true);
				}
		});
		btnThm.setIcon(new ImageIcon(KhachHang.class.getResource("/icon/add-icon.png")));
		btnThm.setBounds(16, 23, 142, 43);
		panel.add(btnThm);

		JButton btnSa = new JButton("Tìm - Sửa");
		btnSa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SuaKhachHang a = new SuaKhachHang();
				a.setVisible(true);
			}
		});
		btnSa.setIcon(new ImageIcon(KhachHang.class.getResource("/icon/edit-icon.png")));
		btnSa.setBounds(174, 23, 142, 43);
		panel.add(btnSa);

		JButton btnXa = new JButton("Xóa");
		btnXa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String phanquyen= UserController.getUser(user).getPhanQuyen();
				if (phanquyen.equals("quanly")==true) {
					XoaKhachHang a = new XoaKhachHang();
					a.setVisible(true);
				}else {
					KoDuocQLNV k = new KoDuocQLNV();
					k.setVisible(true);
				}
			}
		});
		btnXa.setIcon(new ImageIcon(KhachHang.class.getResource("/icon/close-icon.png")));
		btnXa.setBounds(332, 23, 142, 43);
		panel.add(btnXa);

		JButton btnRefresh = new JButton("Refresh");
		btnRefresh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				KhachHang k = new KhachHang(user);
				k.setVisible(true);
			}
		});
		btnRefresh.setIcon(new ImageIcon(KhachHang.class.getResource("/icon/Button-Refresh-icon.png")));
		btnRefresh.setBounds(490, 23, 144, 43);
		panel.add(btnRefresh);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(new LineBorder(new Color(184, 207,
				229)), "Danh s\u00E1ch kh\u00E1ch h\u00E0ng",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.textHighlight));
		panel_1.setBounds(10, 112, 650, 307);
		contentPane.add(panel_1);

		java.util.List<model.KhachHang> list = KhachHangController
				.getDSKhachHang();

		Vector<Vector> rowData = new Vector<Vector>();
		Vector<String> name = new Vector<String>();
		name.addElement("STT");
		name.addElement("Mã khách hàng");
		name.addElement("Tên KH");
		name.addElement("Địa chỉ");
		name.addElement("Số điện thoại");
		name.addElement("Điểm tích lũy");
		Vector v;
		NumberFormat n = NumberFormat.getInstance();
		for (int i = 0; i < list.size(); i++) {
			v = new Vector();
			v.add(i + 1);
			v.add(list.get(i).getMaKH());
			v.add(list.get(i).getTenKH());
			v.add(list.get(i).getDiaChi());
			v.add(list.get(i).getSdt());
			v.add(n.format(list.get(i).getDiemTichLuy()));
			rowData.add(v);
		}
		System.out.println(list.size());
		panel_1.setLayout(null);
		JTable table = new JTable(rowData, name);
		JScrollPane pane = new JScrollPane(table);
		pane.setBounds(12, 23, 626, 272);
		panel_1.add(pane);
	}
}
